﻿using AutoMapper;
using MediatR;
using RBAC_Uthorization.Domain;
using RBAC_Uthorization.Domain.Entity;
using RBAC_Uthorization.Infrstructre.Interface;
using RBAC_Uthorization.Read.API.Application.Command.UserInfoCommand;
using RBAC_Uthorization.Read.API.DTO;
using RBAC_Uthorization.Read.API.DTO.Request;

namespace RBAC_Uthorization.Read.API.Application.CommandHandlers.UserInfoHandler
{
    public class UserInfoShowCommandHandlers : IRequestHandler<UserInfoShowCommand, ApiResult<PagingDto>>
    {
        private readonly IUserInfoRepository _userInfoRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<UserInfoShowCommandHandlers> _logger;

        public UserInfoShowCommandHandlers(ILogger<UserInfoShowCommandHandlers> logger, IMapper mapper, IUserInfoRepository userInfoRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _userInfoRepository = userInfoRepository;
        }

        public async Task<ApiResult<PagingDto>> Handle(UserInfoShowCommand request, CancellationToken cancellationToken)
        {
            try
            {
                List<sys_user> user;
                if (request.offlc_id == "10086")
                {
                    user = await _userInfoRepository.GetAllAsync();
                }
                else
                {
                    user = await _userInfoRepository.GetAllAsunc(x => x.offic_id == request.offlc_id && x.del_flag == 0&&x.sys_id==request.sys_id);
                }
                var list = from x in user
                           where x.del_flag == 0
                           select new
                           {
                               Id = x.id,
                               x.login_name,
                               x.no,
                               x.name,
                               x.email,
                               x.phone,
                               x.user_type,
                               x.Sex,
                               x.mgr_type,
                               x.avatar,
                               x.login_ip,
                               x.login_date,
                               x.login_flag,
                               x.create_by,
                               x.create_date,
                               x.update_by,
                               x.update_date,
                           };
                
                if (!string.IsNullOrEmpty(request.phone))
                {
                    list = list.Where(x => x.phone == request.phone);
                }
                if (!string.IsNullOrEmpty(request.name))
                {
                    list = list.Where(x => x.name.Contains(request.name));
                }
                if (!string.IsNullOrEmpty(request.no))
                {
                    list = list.Where(x => x.no == request.no);
                }
                if (request.login_flag != -1)
                {
                    list = list.Where(x => x.login_flag == request.login_flag);
                }
                var pagedList = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                var result = new PagingDto
                {
                    TotalPage = (int)Math.Ceiling((double)list.Count() / request.pageSize),
                    TotalCount = list.Count(),
                    List = pagedList
                };
                return new ApiResult<PagingDto> { Data = result };
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "用户查询异常");
                return new ApiResult<PagingDto> { Code = ResultCode.Fail, Message = "用户查询异常,请稍后重试" };
            }
        }
    }
}
